package com.tanggh.action;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.springframework.beans.factory.annotation.Autowired;

import com.tanggh.entity.City;
import com.tanggh.entity.Lists;
import com.tanggh.entity.Province;
import com.tanggh.service.ProvinceService;
import com.tanggh.util.JsonUtils;
import com.tanggh.util.XMLUtils;

public class ProvinceAction extends GenericAction {

	@Autowired
	private ProvinceService provinceService;

	@Override
	public String execute() {
		List<Province> list = provinceService.listAll();
		for (Province p : list) {
			System.out.println(p.getName());
			List<City> citys = p.getCitys();
			if (null != citys) {
				for (City c : citys) {
//					System.out.println(" ->" + c.getName());
				}
			}
		}
		String s = JsonUtils.toJson(list);
//		System.out.println(s);

		Lists<Province> lists = new Lists<Province>();
		lists.getValues().addAll(list);
		try {
		/*	File file = new File("f:\\province.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(Lists.class,Province.class);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
			// output pretty printed
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");
			jaxbMarshaller.marshal(lists, file);
			jaxbMarshaller.marshal(lists, System.out);*/
			 XMLUtils.toXML(lists,Province.class, "f:\\province.xml");
			System.out.println("ok");
		} catch (JAXBException e) {
			e.printStackTrace();
		}

		return null;
	}
}
